package com.example.activity;

import java.util.Set;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.R;
import com.example.entity.Capture;
import com.example.entity.CaptureList;
import com.example.module.linker.FacebookLink;
import com.example.module.linker.KakaoLink;
import com.example.module.linker.TwitterLink;
import com.example.persistence.SwitchActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

public class CaptureDetailActivity extends SwitchActivity {
	
    private ViewPager pager;
	private DisplayImageOptions options;
	private CaptureList captureList;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_capture_detail);
		
		pager = (ViewPager) findViewById(R.id.pager);
		wrapContentView((FrameLayout) findViewById(R.id.container), pager);

		captureList = application.getCaptureList();

		Bundle bundle = getIntent().getExtras();
		int pagerPosition = bundle.getInt(IMAGE_POSITION, 0);

		options = new DisplayImageOptions.Builder()
			.showImageForEmptyUri(R.drawable.image_for_empty_url)
			.cacheOnDisc()
			.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
			.build();

		pager.setAdapter(new ImagePagerAdapter());
		pager.setCurrentItem(pagerPosition);
	}
	
	private class ImagePagerAdapter extends PagerAdapter {

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView((View) object);
		}

		@Override
		public void finishUpdate(View container) {
		}

		@Override
		public int getCount() {
			return captureList.size();
		}

		@Override
		public Object instantiateItem(View view, int position) {
			final View imageLayout = getLayoutInflater().inflate(R.layout.item_pager, null);
			final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
			final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
			
			imageLoader.displayImage(captureList.get(position).getPath(), imageView, options, new ImageLoadingListener() {
				@Override
				public void onLoadingStarted() {
					spinner.setVisibility(View.VISIBLE);
				}

				@Override
				public void onLoadingFailed(FailReason failReason) {
					String message = null;
					switch (failReason) {
						case IO_ERROR:
							message = "Input/Output error";
							break;
						case OUT_OF_MEMORY:
							message = "Out Of Memory error";
							break;
						case UNKNOWN:
							message = "Unknown error";
							break;
					}
					Toast.makeText(CaptureDetailActivity.this, message, Toast.LENGTH_SHORT).show();

					spinner.setVisibility(View.GONE);
					imageView.setImageResource(android.R.drawable.ic_delete);
				}

				@Override
				public void onLoadingComplete(Bitmap loadedImage) {
					spinner.setVisibility(View.GONE);
					Animation anim = AnimationUtils.loadAnimation(CaptureDetailActivity.this, R.anim.fade_in);
					imageView.setAnimation(anim);
					anim.start();
				}

				@Override
				public void onLoadingCancelled() {
					// Do nothing
				}
			});

			((ViewPager) view).addView(imageLayout, 0);
			return imageLayout;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view.equals(object);
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View container) {
		}
	}

	@Override
	protected void shareKakaoTalk(Set<String> selectedItem) {
		KakaoLink kakaoLink = KakaoLink.getLink(getApplicationContext());
		if (!kakaoLink.isAvailableIntent()) {
			alert("Not installed KakaoTalk.");		
			return;
		}
		
		Intent kakaoIntent = kakaoLink.getIntentForSendImage(captureList.get(pager.getCurrentItem()).getPath());
		startActivityForResult(kakaoIntent, KAKAO_TALK_CODE);		
	}

	@Override
	protected void shareTwitter(Set<String> selectedItem) {
		Capture capture = captureList.get(pager.getCurrentItem());
		
		TwitterLink twitterLink = new TwitterLink(this);
		twitterLink.publish("사진 공유해요~", getLocalFile(capture.getPath()));
	}
	
	@Override
	protected void shareFacebook(Set<String> selectedItem) {
		Capture capture = captureList.get(pager.getCurrentItem());
		
		FacebookLink facebookLink = new FacebookLink(this);
		facebookLink.publish("사진 공유해요~", getLocalFile(capture.getPath()));		
	}
	
	@Override
	protected void doDelete(Set<String> selectedItem) {
		Capture capture = captureList.get(pager.getCurrentItem());		
		captureList.removeItem(capture.getPath());
		deleteLocalFile(capture.getPath());
//		pager.setCurrentItem(0);
		
		((ImagePagerAdapter) pager.getAdapter()).notifyDataSetChanged();
	}
}
